<?php

/**
 * @copyright since 10:22 25/3/2020
 * @author <mc@dancis.info>
 */

require '../../vendor/autoload.php';

function n_between($val, $lower = 60, $upper = 80)
{
    if (is_numeric($val) && is_numeric($lower) && is_numeric($upper)) {
        if ($lower <= $upper) {
            return $lower <= $val && $val < $upper;
        } else {
            throw new RuntimeException('Lower level is larger than upper level');
        }
    } else {
        throw new RuntimeException('One of params is NaN at least');
    }
}

/**
 * @param $score
 * @return int
 * @todo `?` 會匹配最遠的 `:`
 */
function situation($score)
{
    return $score >= 90 ? 3 : (
        n_between($score, 80, 90) ? 2 : (
            n_between($score, 60, 80) ? 1 : 0
        )
    );
}

function mode_1(&$score)
{
    if ($score >= 90) {
        disp('Excellent');
    } elseif (n_between($score, 80, 90)) {
        disp('Good');
    } elseif (n_between($score, 60, 80)) {
        disp('Passed');
    } else {
        disp('Failed');
    }
}

function mode_2(&$score)
{
    $score = situation($score);
    switch ($score) {
        case 3:
            disp('Excellent');
            break;
        case 2:
            disp('Good');
            break;
        case 1:
            disp('Passed');
            break;
        case 0:
        default:
            disp('Failed');
    }
}
function main($mode)
{
    $score = input('Please enter the score: ');

    $mode = 'mode_' . $mode;
    $mode($score);
}

main(2); // Mode configuration is here
